import LanguageMng from "./LanguageMng";
import LanguageMngTs from "./LanguageMngTs";
import KYCEnum from "../Static/KYCEnum";
import GameDataUtilTs from "../Utils/GameDataUtilTs";
import ApkVersionUtil from "../Utils/ApkVersionUtil";

cc.Class({
    extends: cc.Component,

    properties: {
        mobileEditNode:{
            default:null,
            type:cc.Node
        },
        mobileShowNode:{
            default:null,
            type:cc.Node
        },
        otpEditText:{
            default:null,
            type:cc.EditBox
        },
        optBtn:{
            default:null,
            type:cc.Button,
        },
        otpBtnLabel:{
            default:null,
            type:cc.Label
        },
        thumbMobileNode:{
            default:null,
            type:cc.Node
        },
        labelThumbMobile:{
            default:null,
            type:cc.Label
        },
        labelThumbUID:{
            default:null,
            type:cc.Label
        },

        ebMobile:{
            default:null,
            type:cc.EditBox
        },

        labelEmail:{
            default:null,
            type:cc.EditBox
        },
        labelFullName:{
            default:null,
            type:cc.EditBox
        },
        labelPanNum:{
            default:null,
            type:cc.EditBox
        },
        labelState:{
            default:null,
            type:cc.Label
        },
        // labelBirth:{
        //     default:null,
        //     type:cc.EditBox
        // },
        // labelState:{
        //     default:null,
        //     type:cc.EditBox
        // },
        yesPan:{
            default:null,
            type:cc.Node
        },
        cbbYear:{
            default:null,
            type:require("ComboBox")
        },
        cbbMonth:{
            default:null,
            type:require("ComboBox")
        },
        cbbDay:{
            default:null,
            type:require("ComboBox")
        },
        cbbProvince:{
            default:null,
            type:require("ComboBox")
        },

        allContentNode:{
            default:null,
            type:cc.Node
        },

        spinnerBlock:{
            default:null,
            type:cc.Node
        },

        statusLabel:{
            default:null,
            type:cc.Label
        },

        tagNameError:{
            default:null,
            type:cc.Node
        },

        tagDobError:{
            default:null,
            type:cc.Node
        },

        //用于提示用户验证失败
        _showErrorDialog : false,
    },

    // LIFE-CYCLE CALLBACKS:

    async onLoad () {

        let DataModel = {
            YEAR : LanguageMngTs.getSentence(366)+" *", //Year
            MONTH:LanguageMngTs.getSentence(367)+" *",  //Month
            DAY:LanguageMngTs.getSentence(173)+" *",    //Day
        }
        let StateModel = {
            NONE:LanguageMngTs.getSentence(368)+" *"   //None
        }

        this.dataModel = DataModel
        this.stateModel = StateModel

        this.tagNameError.active = false
        this.tagDobError.active = false
        this.statusLabel.string = ""

        let playerInfo = null
        let playerMobile = playerInfo ? playerInfo.mobile_no : null
        if(playerMobile){
            this.ebMobile.string = playerMobile
        }

        this.spinnerBlock.active = false

        let hor = GameDataUtilTs.getIsAllHorizontal() || false
        let ver = ApkVersionUtil.getApkVersionCode() || 0
        if(!hor){
            if(ver < 10003){
                // cc.gg.gameDataUtil.addKeyboardUpCb("kycup",function (keyboardHeight) {
                //     if(this._focusEb){
                //         let minus = this._focusEb.y - keyboardHeight + keyboardHeight - 50
                //         this.allContentNode.y -= minus
                //         this._contentOffsetY = minus
                //     }
                // }.bind(this))
                //
                // cc.gg.gameDataUtil.addKeyboardDownCb("kycdown",function (keyboardHeight) {
                //     if(this._focusEb){
                //         this.allContentNode.y += this._contentOffsetY
                //         this._focusEb = null
                //     }
                // }.bind(this))
            }else {
                //什么都不做，因为键盘不会把内容顶上去
            }
        }else {
            // cc.gg.gameDataUtil.addKeyboardUpCb("kycup",function (keyboardHeight) {
            //     if(this._focusEb && keyboardHeight > this._focusEb.y){
            //         this.upMoved = true
            //         let minus = keyboardHeight - this._focusEb.y + 20
            //         this.allContentNode.y += minus
            //         this._contentOffsetY = minus
            //     }
            // }.bind(this))
            //
            // cc.gg.gameDataUtil.addKeyboardDownCb("kycdown",function (keyboardHeight) {
            //     if(this._focusEb && this.upMoved){
            //         this.upMoved = false
            //         this.allContentNode.y -= this._contentOffsetY
            //         this._focusEb = null
            //     }
            // }.bind(this))
        }


        await cc.gg.toastMgr.showNetWaitting()
        let result = await cc.gg.httpUtil.httpGet('/user/doc/kyc/get',null)
        cc.gg.toastMgr.removeNetWaitting()
        if(!result.succ){
            cc.error(result.msg)
        }else {
            let kycData = result.data
            cc.gg.dataMgr.kycData = kycData
            this._initKYCDetail(kycData)
        }

        this._panUploaded = this._onPanUploaded.bind(this)
        cc.find("Canvas").on("kyc_update_pan_updated",this._panUploaded,this)

        let startData = new Date()
        var year=startData.getFullYear();
        var month=startData.getMonth()+1;
        var date=startData.getDate();
        let yearArray = []
        for(let i=2002;i>1950;i--){
            yearArray.push(i.toString())
        }
        let monthArray = []
        for(let i=1;i<13;i++){
            monthArray.push(i.toString())
        }
        let dayArray = []
        for(let i=1;i<32;i++){
            dayArray.push(i.toString())
        }
        let province = ['Andaman and Nicobar Islands',
            'Andra Pradesh',
            'Arunachal Pradesh',
            // 'Assam',
            'Bihar',
            'Chandigarh',
            'Chhattisgarh',
            'Dadar and Nagar Haveli',
            'Daman and Diu',
            'Delhi',
            'Goa',
            'Gujarat',
            'Haryana',
            'Himachal Pradesh',
            'Jammu and Kashmir',
            'Jharkhand',
            'Karnataka',
            'Kerala',
            'Lakshadeep',
            'Madya Pradesh',
            'Maharashtra',
            'Manipur',
            'Meghalaya',
            'Mizoram',
            // 'Nagaland',
            // 'Orissa',
            'Pondicherry',
            'Punjab',
            'Rajasthan',
            // 'Sikkim',
            'Tamil Nadu',
            // 'Telagana',
            'Tripura',
            'Union Territories - 7',
            'Uttar Pradesh',
            'Uttaranchal',
            // 'West Bengal'
        ]
        this.cbbYear.initComboBox(yearArray,this._openBgBlock.bind(this),this._closeBgBlock.bind(this))
        this.cbbMonth.initComboBox(monthArray,this._openBgBlock.bind(this),this._closeBgBlock.bind(this))
        this.cbbDay.initComboBox(dayArray,this._openBgBlock.bind(this),this._closeBgBlock.bind(this))
        this.cbbProvince.initComboBox(province,this._openBgBlock.bind(this),this._closeBgBlock.bind(this))

        this._initUI()
    },

    _initUI(){

        //0未验证 1验证中 2验证失败 3验证通过
        let status = cc.gg.dataMgr.kycData.status
        if(status === KYCEnum.NONE){
            this.statusLabel.string = ""
        }else if(status === KYCEnum.VERIFICATING){
            this.statusLabel.string = "(Verificating)"
            this.statusLabel.node.color = new cc.Color(255,255,60)
        }else if(status === KYCEnum.PASSED){
            this.statusLabel.string = "(Passed)"
            this.statusLabel.node.color = new cc.Color(0,255,0)
        }else{
            this.statusLabel.string = "(Not pass)"
            this.statusLabel.node.color = new cc.Color(255,0,0)
            this._showErrorDialog = true

            if(status === KYCEnum.NAME_ERROR){
                this.tagNameError.active = true
            }else if(status === KYCEnum.BIRTH_ERROR){
                this.tagDobError.active = true
            }else {
                cc.warn("Unknown code.")
            }
        }
    },

    _openBgBlock:function (comboBox) {
        this.spinnerBlock.active = true
        this.closeAllSpinner(comboBox)
    },

    _closeBgBlock:function (comboBox) {
        this.spinnerBlock.active = false
    },

    onSpinnerBlockClicked:function (event) {
        this.spinnerBlock.active = false
        this.closeAllSpinner(null)
    },

    closeAllSpinner:function (comboBox) {
        if(comboBox!==this.cbbYear) this.cbbYear.hideDropDownBox()
        if(comboBox!==this.cbbMonth) this.cbbMonth.hideDropDownBox()
        if(comboBox!==this.cbbDay) this.cbbDay.hideDropDownBox()
        if(comboBox!==this.cbbProvince) this.cbbProvince.hideDropDownBox()
    },

    onDestroy(){
        // let canvas = cc.find("Canvas")
        // if(canvas)
        // cc.find("Canvas").off("kyc_update_pan_updated",this._panUploaded,this)
    },

    start () {
        if(this._showErrorDialog){
            this._showErrorDialog = false
            this._dialogErrorNotice()
        }
    },

    _dialogErrorNotice(){
        let title = LanguageMng.getSentence(3) //Notice
        let btnStr = LanguageMng.getSentence(181)   //ok
        let contentStr = LanguageMng.getSentence(240)   //Your KYC verification failed, please check the items with red * and submit again
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    },

    onNeedFitEditBoxClicked:function (event) {
        let ebNode = event.node
        if(ebNode === this.labelEmail.node){
            this._focusEb = this.labelEmail.node.parent
        }else if(ebNode === this.labelFullName.node){
            this._focusEb = this.labelFullName.node.parent
        }else if(ebNode === this.labelPanNum.node){
            this._focusEb = this.labelPanNum.node.parent
        }else if(ebNode === this.ebMobile.node){
            this._focusEb = this.ebMobile.node.parent
        }
    },

    // onOtpSendButtonClicked:function () {
    //     let mobile = this.mobileEditText.string
    //
    //     if(mobile.indexOf("91") !== 0){
    //         mobile = "91" + mobile
    //     }
    //
    //     if(!this.checkMobile(mobile)){
    //         cc.gg.toastMgr.showToast("Mobile number is not correct.")
    //         return
    //     }
    //
    //     this.startOtpCountDown(60)
    //     if(this._resent){
    //         cc.gg.httpUtil.httpPost("/user/pin/resent",{mobileNo:mobile,pinId:this.pinId},function (result) {
    //             if (!result.succ) {
    //                 cc.gg.toastMgr.showToast("otp send failed.")
    //                 cc.error(`验证码发送失败: ${result.msg}`);
    //                 return;
    //             }
    //
    //             cc.log("pincode::::::::::::::::",result,"  ",result.data.pinId)
    //         }.bind(this))
    //     }else {
    //         cc.gg.httpUtil.httpPost("/user/pin/send",{mobileNo:mobile},function (result) {
    //             if (!result.succ) {
    //                 cc.gg.toastMgr.showToast("otp send failed.")
    //                 cc.error(`验证码发送失败: ${result.msg}`);
    //                 return;
    //             }
    //
    //             this._resent = true
    //             this.pinId = result.data.pinId
    //             cc.log("pincode::::::::::::::::",result.data.pinId)
    //         }.bind(this))
    //     }
    // },

    startOtpCountDown:function (seconds) {
        this.maxCount = seconds
        this.otpBtnLabel.node.color = new cc.Color(200,200,200)
        this.optBtn.interactable = false
        this.countDownHandler = function () {
            this.maxCount--
            if(this.maxCount === 0){
                this.otpBtnLabel.node.color = new cc.Color(0,0,0)
                this.optBtn.interactable = true
                this.otpBtnLabel.string = "Send again"
                this.otpBtnLabel.node.color = new cc.Color(255,255,255)
                this.unschedule(this.countDownHandler)
                return
            }
            this.otpBtnLabel.string = "Send again("+this.maxCount+")"
        }.bind(this)
        this.schedule(this.countDownHandler, 1)
    },

    _onPanUploaded:function (resJson) {
        // cc.gg.toastMgr.showNetWaitting()
        this.yesPan.active = true

        // cc.gg.toastMgr.removeNetWaitting()
        this._panImg = resJson.data.img
        this.yesPan.active = this._panImg ? true : false

        // this.refreshUserKYCData(function (kycData) {
        //     //show pan card tag icon
        //     cc.gg.toastMgr.removeNetWaitting()
        //     this._panImg = kycData.panImg
        // }.bind(this))
    },

    onKYCTestButtonClickedUp:function (event) {
        if(this._focusEb){
            let minus = this._focusEb.y - 800
            this.allContentNode.y -= minus
            this._contentOffsetY = minus
        }
    },

    onKYCTestButtonClickedDown:function (event) {
        if(this._focusEb){
            this.allContentNode.y += this._contentOffsetY
            this._focusEb = null
        }
    },

    refreshUserKYCData:function (cb) {
        cc.log("刷新全局KYCData！")
        cc.gg.httpUtil.httpGet('/user/doc/kyc/get',null,function (result) {
            if(!result.succ){
                cc.error(result.msg)
            }else {
                cc.log("新的KYCdata：",result.data)
                cc.gg.dataMgr.kycData = result.data
                if(cb) cb(result.data)
            }
        }.bind(this))
    },
    // update (dt) {},

    _initKYCDetail:function (data) {
        let mobile = data.mobile_no
        let userid = cc.gg.dataMgr.getPlayerInfo().uid
        let pannumber = data.pan_number
        let emailaddr = data.email_addr
        let fullname = data.full_name
        let birthday = data.dob //birth day
        let state = data.state
        let pan_img = data.pan_img
        this._panImg = pan_img

        if(mobile){
            cc.log("有手机号，显示注册了的手机号")
            this.showMobile(mobile)
        }

        this.yesPan.active = pan_img ? true : false
        this.labelThumbUID.string = userid
        this.labelPanNum.string = pannumber || ""
        this.labelEmail.string = emailaddr || ""
        this.labelFullName.string = fullname || ""
        this.labelState.string = cc.gg.baseUtil.isValidString(state) ? state : this.stateModel.NONE
        this._initDobView(birthday)


        //用于检测用户是否有更改
        this._oriMobile = mobile
        this._oriEmail = emailaddr
        this._oriFullName = fullname
        this._oriPanCode = pannumber
        this._oriDob = birthday
        this._oriState = state
        this._oriPanImg = pan_img
    },

    _initDobView(birthday){
        let exsistsDob = cc.gg.baseUtil.isValidString(birthday) ? true : false
        birthday = birthday ? birthday : ""

        let dateArr = null
        let year = null
        let month = null
        let day = null

        if(exsistsDob){
            let unknownDob = false
            if(birthday.indexOf("-") != -1){    //兼容旧的数据
                dateArr = birthday.split("-")
                year = dateArr[0]
                month = dateArr[1]
                day = dateArr[2]
            }else if(birthday.indexOf("/") != -1){
                dateArr = birthday.split("/")
                year = dateArr[2]
                month = dateArr[1]
                day = dateArr[0]
            }else {
                unknownDob = true
                cc.warn("未知的DOB格式！",birthday)
            }

            if(!unknownDob){
                year = parseInt(year)
                month = parseInt(month)
                day = parseInt(day)
            }
        }else {
            year = this.dataModel.YEAR
            month = this.dataModel.MONTH
            day = this.dataModel.DAY
        }

        this.cbbYear.getComponent("ComboBox").comboLabel.string = year
        this.cbbMonth.getComponent("ComboBox").comboLabel.string = month
        this.cbbDay.getComponent("ComboBox").comboLabel.string = day
    },

    onKYCImageUploadClicked:function (event) {
        cc.gg.baseUtil.openGalary(cc.gg.httpUtil.panImgUrl,1,cc.gg.httpUtil.USER_TOKEN)
    },

    _checkIlligal(mobileNo,emailAddr,panNumber,state){

        if(mobileNo && !this.checkMobile(mobileNo)){
            cc.gg.toastMgr.showToast("Mobile number is not correct.")
            return false
        }

        if(emailAddr && !this.checkEmail(emailAddr)){
            cc.gg.toastMgr.showToast("E-mail address is not correct.")
            return false
        }
        if(panNumber && !this.checkPanNumber(panNumber)){
            cc.gg.toastMgr.showToast("Pan number is not correct.")
            return false
        }
        if(state === this.stateModel.NONE){
            cc.error("State is not correct.")
            return false
        }

        return true
    },

    async saveContent(){
        let mobileNo = cc.gg.baseUtil.isValidString(this.ebMobile.string) ? this.ebMobile.string : null
        let emailAddr = cc.gg.baseUtil.isValidString(this.labelEmail.string) ? this.labelEmail.string : null
        let fullName = cc.gg.baseUtil.isValidString(this.labelFullName.string) ? this.labelFullName.string : null
        let panNumber = cc.gg.baseUtil.isValidString(this.labelPanNum.string) ? this.labelPanNum.string : null
        let yearDate = this.cbbYear.getComponent("ComboBox").comboLabel.string
        let monthDate = this.cbbMonth.getComponent("ComboBox").comboLabel.string
        let dayDate = this.cbbDay.getComponent("ComboBox").comboLabel.string
        let dob = null
        if(yearDate !== this.dataModel.YEAR && monthDate !== this.dataModel.MONTH && dayDate !== this.dataModel.DAY){
            monthDate = monthDate.indexOf("0") === 0 ? monthDate : parseInt(monthDate) > 10 ? monthDate : "0"+monthDate
            dayDate = dayDate.indexOf("0") === 0 ? dayDate : parseInt(dayDate) > 10 ? dayDate : "0"+dayDate
            dob = dayDate + "/" + monthDate + "/" + yearDate
        }
        let state = this.cbbProvince.getComponent("ComboBox").comboLabel.string
        if(!cc.gg.baseUtil.isValidString(state) || state === this.stateModel.NONE) state = null

        if(mobileNo && mobileNo.indexOf("91") !== 0){
            mobileNo = "91" + mobileNo
        }

        if(!this._checkIlligal(mobileNo,emailAddr,panNumber,state)){
            return
        }

        cc.log("保存电话：",mobileNo)
        cc.log("保存emailAddr：",emailAddr)
        cc.log("保存fullName：",fullName)
        cc.log("保存panNumber：",panNumber)
        cc.log("保存生日：",dob)
        cc.log("保存地区：",state)

        cc.gg.toastMgr.showNetWaitting()
        let panImg = this._panImg || "" //"https://static.khelochalo.com/panimg_uid.png"
        cc.log("测试pan卡图片！",this._panImg)
        let result = await cc.gg.httpUtil.httpPost("/user/doc/kyc/update",{mobileNo,emailAddr,fullName,panNumber,dob,state,panImg})
        if (!result.succ) {
            cc.gg.toastMgr.removeNetWaitting()
            cc.gg.toastMgr.showToast(result.msg)
        }else {
            // cc.gg.gameDataUtil.goToLobby(true)
            this.node.destroy()
        }
    },

    _checkContentChanged(){
        let mobileNo = cc.gg.baseUtil.isValidString(this.ebMobile.string) ? this.ebMobile.string : null
        let emailAddr = cc.gg.baseUtil.isValidString(this.labelEmail.string) ? this.labelEmail.string : null
        let fullName = cc.gg.baseUtil.isValidString(this.labelFullName.string) ? this.labelFullName.string : null
        let panNumber = cc.gg.baseUtil.isValidString(this.labelPanNum.string) ? this.labelPanNum.string : null
        let yearDate = this.cbbYear.getComponent("ComboBox").comboLabel.string
        let monthDate = this.cbbMonth.getComponent("ComboBox").comboLabel.string
        let dayDate = this.cbbDay.getComponent("ComboBox").comboLabel.string
        let dob = null
        if(yearDate !== this.dataModel.YEAR && monthDate !== this.dataModel.MONTH && dayDate !== this.dataModel.DAY){
            monthDate = monthDate.indexOf("0") === 0 ? monthDate : parseInt(monthDate) > 10 ? monthDate : "0"+monthDate
            dayDate = dayDate.indexOf("0") === 0 ? dayDate : parseInt(dayDate) > 10 ? dayDate : "0"+dayDate
            dob = dayDate + "/" + monthDate + "/" + yearDate
        }
        let state = this.cbbProvince.getComponent("ComboBox").comboLabel.string
        if(state === this.stateModel.NONE) state = null

        let difMobile = mobileNo !== this._oriMobile
        let difEmail = emailAddr !== this._oriEmail
        let difFullName = fullName !== this._oriFullName
        let difPanNum = panNumber !== this._oriPanCode
        let difDob = dob !== this._oriDob
        let difState = state !== this._oriState
        let difPanImg = this._panImg !== this._oriPanImg

        if(difMobile || difEmail || difFullName || difPanNum || difDob || difState || difPanImg){
            return true
        }else {
            return false
        }
    },

    async onKYCCloseButtonClicked (event) {
        if(this._checkContentChanged()){
            let title = LanguageMng.getSentence(3)  //Notice
            let positiveStr = LanguageMng.getSentence(57)   //Yes
            let passiveStr = LanguageMng.getSentence(56)   //No
            let content = LanguageMngTs.getSentence(241)//Your KYC data is changed,are you sure to discard your changes?
            cc.gg.toastMgr.showDialog(title,positiveStr,passiveStr,content,
                function () {
                    // cc.gg.gameDataUtil.goToLobby(true)
                    cc.gg.toastMgr.removeDialog()
                    this.node.destroy();
                }.bind(this),
                function () {
                    cc.gg.toastMgr.removeDialog()
                }
            )
        }else{
            // cc.gg.gameDataUtil.goToLobby(true)
            this.node.destroy();
            
        }
        // if(this._checkContentChanged()){
        //     let title = LanguageMng.getSentence(3)  //Notice
        //     let positiveStr = LanguageMng.getSentence(57)   //Yes
        //     let passiveStr = LanguageMng.getSentence(56)   //No
        //     let content = "You have changed kyc data,do you want to restore them?"
        //     cc.gg.toastMgr.showDialog(title,positiveStr,passiveStr,content,
        //         function () {
        //             cc.gg.toastMgr.removeDialog()
        //             this.saveContent()
        //         }.bind(this),
        //         function () {
        //             cc.gg.gameDataUtil.goToLobby(true)
        //         }
        //     )
        // }else {
        //     cc.gg.gameDataUtil.goToLobby(true)
        // }
    },

    _showVerifySuccessDialog(){
        let title = LanguageMng.getSentence(186) //KYC Verification
        let btnStr = LanguageMng.getSentence(181)   //ok
        let contentStr = LanguageMng.getSentence(242)   //KYC verification passed!
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
            // cc.gg.gameDataUtil.goToLobby(true)
            this.node.destroy()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    },

    _showAskWaitVerifyDialog(){
        let title = LanguageMng.getSentence(186) //KYC Verification
        let btnStr = LanguageMng.getSentence(181)   //ok
        let contentStr = LanguageMng.getSentence(237)   //Your KYC certification data has been submitted, please wait for the audit results patiently
        let cb = function () {
            cc.gg.toastMgr.removeDialog()
            // cc.gg.gameDataUtil.goToLobby(true)
            this.node.destroy()
        }.bind(this)
        cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    },

    // _showAskWaitVerifyDialog(){
    //     let title = LanguageMng.getSentence(186) //KYC Verification
    //     let btnStr = LanguageMng.getSentence(181)   //ok
    //     let contentStr = LanguageMng.getSentence(237)   //Your KYC certification data has been submitted, please wait for the audit results patiently
    //     let cb = function () {
    //         cc.gg.toastMgr.removeDialog()
    //         cc.gg.gameDataUtil.goToLobby(true)
    //     }.bind(this)
    //     cc.gg.toastMgr.showDialogOneButton(title,btnStr,contentStr,cb)
    // },

    onKYCBindMobileClicked:function (event) {
        let self = this
        cc.loader.loadRes("windows/bindMobile",cc.Prefab,function (err, prefab) {
            let newBindWin = cc.instantiate(prefab)
            newBindWin.getComponent("BindMobile").initBindMobileWindow(function (mobileStrWithArea) {
                self.showMobile(mobileStrWithArea)
            }.bind(this))
            newBindWin.y = 200
            cc.find("Canvas").addChild(newBindWin)
        }.bind(this))
    },

    showMobile:function (number) {
        this.ebMobile.string = number
        // this.thumbMobileNode.active = true
        // this.labelThumbMobile.string = number
        this.mobileEditNode.active = false
        // this.mobileShowNode.active = true
    },

    // onKYCSubmitButtonClicked:function (event) {
    //     if(this.mobileEditText.string && this.mobileEditText.string!=""
    //     && this.otpEditText.string && this.otpEditText.string!=""){
    //         this.sendOtp(function () {
    //             this.submitTextInfo()
    //         }.bind(this))
    //     }else {
    //         this.submitTextInfo()
    //     }
    // },


    async submitTextInfo () {

        let mobileNo = this.ebMobile.string
        let emailAddr = this.labelEmail.string
        let fullName = this.labelFullName.string
        let panNumber = this.labelPanNum.string
        let yearDate = this.cbbYear.getComponent("ComboBox").comboLabel.string
        let monthDate = this.cbbMonth.getComponent("ComboBox").comboLabel.string
        let dayDate = this.cbbDay.getComponent("ComboBox").comboLabel.string
        // let dob = yearDate + "-" + monthDate + "-" + dayDate
        let state = this.cbbProvince.getComponent("ComboBox").comboLabel.string
        cc.log("提交电话：",mobileNo)
        cc.log("提交emailAddr：",emailAddr)
        cc.log("提交fullName：",fullName)
        cc.log("提交panNumber：",panNumber)
        cc.log("提交地区：",state)

        // if(!this.checkMobile(mobileNo)){
        //     cc.gg.toastMgr.showToast("Mobile number is not correct.")
        //     return
        // }

        if(!cc.gg.baseUtil.isAllNumber(yearDate) || !cc.gg.baseUtil.isAllNumber(monthDate) || !cc.gg.baseUtil.isAllNumber(dayDate)){
            cc.gg.toastMgr.showToast("Please select your Birthday.")
            return
        }

        yearDate = parseInt(yearDate)
        monthDate = parseInt(monthDate) >= 10 ? monthDate : "0"+monthDate
        dayDate = parseInt(dayDate) >= 10 ? dayDate : "0"+dayDate
        let dob = dayDate + "/" + monthDate + "/" + yearDate
        cc.log("提交生日：",dob)

        if(state == "None *"){
            cc.gg.toastMgr.showToast("Please select your state.")
            return
        }

        if(mobileNo.indexOf("91") !== 0){
            mobileNo = "91" + mobileNo
        }

        if(emailAddr && !this.checkEmail(emailAddr)){
            cc.gg.toastMgr.showToast("E-mail address is not correct.")
            return
        }

        if(panNumber && !this.checkPanNumber(panNumber)){
            cc.gg.toastMgr.showToast("Pan number is not correct.")
            return
        }

        if(!cc.gg.baseUtil.isAllNumber(yearDate) || !cc.gg.baseUtil.isAllNumber(monthDate) || !cc.gg.baseUtil.isAllNumber(dayDate)){
            cc.gg.toastMgr.showToast("Birthday is not correct.")
            return
        }

        cc.gg.toastMgr.showNetWaitting()
        let panImg = this._panImg || ""


        let testInfoStr = cc.gg.dataMgr.getPlayerInfo().uid+"+"+mobileNo + "+" + emailAddr + "+" + fullName + "+" +panNumber +"+"+dob+"+"+state
        let kv = {
            keys:["Info"],
            values:[testInfoStr.toString()]
        }
        let jsonStr = JSON.stringify(kv)
        cc.gg.tdUtil.doSendTdEvent("debug_kycinfo","KYC",jsonStr)


        let result = await cc.gg.httpUtil.httpPost("/user/doc/kyc/update",{mobileNo,emailAddr,fullName,panNumber,dob,state,panImg})
        if (!result.succ) {
            cc.gg.toastMgr.removeNetWaitting()
            cc.gg.toastMgr.showToast(result.msg)
            return
        }

        let verify = await this.startVerify()
        cc.gg.toastMgr.removeNetWaitting()

        // if(verify){
        //     // setTimeout(function()
        //     // {
        //     //     cc.gg.gameDataUtil.goToLobby(true)
        //     // }.bind(this),1000)
        //     this._showAskWaitVerifyDialog()
        // }
    },

    async startVerify () {
        cc.gg.toastMgr.showNetWaitting()
        let result = await cc.gg.httpUtil.httpGet("/user/doc/kyc/pan/verify")
        cc.gg.toastMgr.removeNetWaitting()
        if(result.succ){
            let status = result.data.status
            if(status === KYCEnum.VERIFICATING){
                this._showAskWaitVerifyDialog()
            }else if(status === KYCEnum.PASSED){
                this._showVerifySuccessDialog()
            }else {
                cc.warn("未知的提交成功状态码")
            }
            return true
        }else {
            //1=文档未完成 2=名字错误 3=生日错误 2020-5-22与服务端同步
            let code = result.code
            if(code === 1){
                cc.gg.toastMgr.showToast("Your document is not finished.",2)
                return false
            }else if(code === 2){
                cc.gg.toastMgr.showToast("Name verification failed.",2)
                return false
            }else if(code === 3){
                cc.gg.toastMgr.showToast("Birthday verification failed.",2)
                return false
            }else {
                cc.gg.toastMgr.showToast("Verify failed.Please try again.",2)
                return false
            }
            return false
        }
    },

    checkMobile:function (mobileNo) {
        mobileNo = mobileNo.replace(/ /g, '');
        let regx = /^91\d{10}$/;
        if (!regx.test(mobileNo)) return false
        return true
    },

    checkEmail:function (emailAddr) {
        emailAddr = emailAddr.replace(/ /g, '');
        // let regx = /^\w+@[a-zA-Z0-9]{2,10}(?:\.[a-z]{2,4}){1,3}$/;
        let regx = /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/;
        if (!regx.test(emailAddr)) return false
        return true
    },

    checkPanNumber:function (panNumber) {
        panNumber = panNumber.toUpperCase().replace(/ /g, '');
        let regx = /^[A-Z]{5}[0-9]{4}[A-Z]$/;
        if (!regx.test(panNumber)) return false
        return true
    },

    checkBirth:function (birthDate) {
        birthDate = birthDate.replace(/ /g, '');
        let regx = /^\d{4}-[0-1]\d-[0-3]\d$/;
        if (!regx.test(birthDate)) return false
        return true
    },
});
